package servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class Complaint
 */
@WebServlet("/Complaint")
public class Complaint extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Complaint() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    private final String DB_URL = "jdbc:mysql://localhost:3306/complaints";
	private final String DB_USER = "root";
	private final String DB_PASSWORD = "";
	private final String DB_DRIVER = "com.mysql.jdbc.Driver";
	private Statement stmt;
	private ResultSet xrst;
	private Connection con;
	
	private ResultSet getRst(String request){
		try {
	    	Class.forName(DB_DRIVER);
	    	con = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
	    	stmt = null;
	    	stmt = con.createStatement();
	    	xrst = stmt.executeQuery(request);
	    	return xrst;
		}
		catch(Exception e){
			System.out.println(e.getMessage());
			return null;
		}
	}
	
	//seda ei ole veel testinud
	private void exexuteStatement(String statement){
		try {
	    	Class.forName(DB_DRIVER);
	    	con = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
	    	stmt = null;
	    	stmt = con.createStatement();
	    	stmt.executeUpdate(statement);
	    	stmt.close();
			con.close();
		}
		catch(Exception e){
			System.out.println(e.getMessage());
		}
	}
	
	private void closeDatabaseConnection(){
		try {
			stmt.close();
			con.close();
		} catch (SQLException e) {
			System.out.println(e.getMessage());
		}
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("action");
		HttpSession session = request.getSession(true);
		String username =  (String)session.getAttribute("username");
		response.setContentType("text/html");
		
		
		if(action.equals("login")){
			String uName = request.getParameter("username");
			boolean userFound = false;
			if(request.getParameter("username").isEmpty()){
				response.sendRedirect("user/login.html");
			}
			else{
				ResultSet rst = getRst("SELECT * from resolvers");
				try {
					while(rst.next()){
						if (rst.getString("email").equals(uName)){
							session.setAttribute("username", uName);
							session.setAttribute("orderby", "'c.date'");
							userFound = true;
							response.sendRedirect("index.jsp");
						}
					}
				} catch (SQLException e) {
					System.out.print("Error in action == login");
					e.printStackTrace();
				}
				if(!userFound){
					response.sendRedirect("user/login.html");
				}
			}
		}
		
		else if (action.equals("logout")){
			session.removeAttribute("username");
		}
		else if (action.equals("test")){
			ResultSet rst = getRst("SELECT * from test");
			
			try {
				while(rst.next()){
					System.out.println(rst.getString("name"));
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			closeDatabaseConnection();
		}
		else{
			System.out.println("Unknown action: " + action);
		}
	}

}
